package com.zxy.salenet.web.web.controller;

/**
 * @author: zxy
 * @version: 1.0
 * @description:
 */

import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.pojo.SalenetCar;
import com.zxy.salenet.web.pojo.SalenetGoods;
import com.zxy.salenet.web.pojo.dto.BuyGoodsDTO;
import com.zxy.salenet.web.pojo.dto.CarDTO;
import com.zxy.salenet.web.pojo.vo.BuySuccessVO;
import com.zxy.salenet.web.service.UserOperationService;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
    /**
 * UserOperationController
 */
@Api(value = "用户对商品管理API",tags = {"用户对商品管理API"})
@RestController
@RequestMapping(path = "/queryGoods")
public class UserOperationController {
    @Autowired
    private UserOperationService userOperationService;

    /**
     * 根据商品名称查询对应的商品信息
     *
     * @return
     */
    @ApiOperation(value = "根据商品名称查询对应的商品信息 如果商品名称为空则获取所有的商品信息",notes = "根据商品名称获取商品列表,JSON格式:{\"goodsName\":\"\",\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name = "Json",value = "" , dataType = "Json",required = true)
    @PostMapping(path = "/queryByName")
    private Json queryGoodsMessage(@RequestBody Map<String, String> map) {
        String goodsName = map.get("goodsName");
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        Json json = new Json();
        PageInfo<SalenetGoods> pageInfo = userOperationService.queryGoodsByName(goodsName, pageNum, pageSize);
        json.setCode(200);
        json.setData(pageInfo);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 根据商品分类获取商品信息
     *
     * @return
     */
    @ApiOperation(value = "根据商品分类获取对应的商品信息",notes = "根据商品类型获取商品信息 下拉列表中的value的值为商品类型的编号 如 口红对应的编号为 77,JSON格式:{\"goodsType\":77,\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/queryByType")
    private Json queryGoodsByType(@RequestBody Map<String, Integer> map) {
        Json json = new Json();
        Integer goodsType = map.get("goodsType");
        Integer pageNum = map.get("pageNum");
        Integer pageSize = map.get("pageSize");
        PageInfo<SalenetGoods> allGoods = userOperationService.queryGoodsByGoodsType(goodsType, pageNum, pageSize);
        json.setData(allGoods);
        json.setCode(200);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 添加商品到购物车
     *
     * @return
     */
    @ApiOperation(value = "添加商品到购物车中",notes = "将商品添加到购物车中 JSON 格式 { \"carGoodsId\":10082,\n" +
            "\"carGoodsCount\":4,\n" +
            "\"carGoodsName\":\"CHANEL香奈儿炫亮魅力丝绒唇膏狮子系列 口红58\",\n" +
            "\"carGoodsPrice\":1089.00,\n" +
            "\"carGoodsPricecount\":4356.00,\n" +
            "\"carGoodsUserId\":67531}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/addGoodsCar")
    private Json addGoodsToCar(@RequestBody SalenetCar car) {
        Json json = new Json();
        // TODO: 2021/3/7 添加商品信息到购物车
        System.out.println(car);
        Integer result = userOperationService.addGoodsToCar(car.getCarGoodsUserId(), car);
        json.setData(201);
        json.setCode(200);
        json.setMessage("添加成功");
        return json;
    }

    /**
     * 获取购物车信息
     *
     * @return
     */
    @ApiOperation(value = "获取对应账号的购物车信息 发送用户的userId",notes = "获取对应账号的购物车信息  JSON {\"userId\":67531 }")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/queryCar")
    private Json queryCar(@RequestBody Map<String, Integer> map) {
        Json json = new Json();
        Integer userId = map.get("userId");
        List<SalenetCar> carResult = userOperationService.queryCarById(userId);
        json.setCode(200);
        json.setData(carResult);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 修改商品数量
     *
     * @return 获取到的数据
     */
    @ApiOperation(value = "修改商品的购买数量",notes = "修改商品的购买数量 userId 用户id goodsId 商品的编号 currCount 当前商品总数 JSON {\"userId\":67531,\"goodsId\":10082,\"currCount\":2}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/updateCar")
    private Json updateCar(@RequestBody CarDTO carDTO) {
        Json json = new Json();
        Integer updateResult = userOperationService.updateGoodsCount(carDTO.getUserId(), carDTO.getGoodsId(), carDTO.getCurrCount());
        json.setCode(200);
        json.setData(updateResult);
        json.setMessage("修改成功");
        return json;
    }

    /**
     * 删除购物车中的商品
     *
     * @return
     */
    @ApiOperation(value = "删除购物车中的商品",notes = "发送用户的userId 商品的goodsId JSON {\"userId\":67531,\"goodsId\":10082}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/deleteCar")
    private Json deleteCar( @RequestBody Map<String,Integer> map) {
        Json json = new Json();
        Integer userId = map.get("userId");
        Integer goodsId = map.get("goodsId");
        Integer deleteResult = userOperationService.deleteGoodsForCarByGoodsIdAndUserId(userId, goodsId);
        json.setCode(200);
        json.setData(0);
        json.setMessage("删除成功!");
        return json;
    }

    /**
     * 商品购买
     *
     * @return
     */
    @ApiOperation(value = "商品购买下单",notes = "将需要结算的商品进行结算 JSON {[{\"id\": 1,\n" +
            "            \"carGoodsId\": 10082,\n" +
            "            \"carGoodsCount\": 1,\n" +
            "            \"carGoodsName\": \"CHANEL香奈儿炫亮魅力丝绒唇膏狮子系列 口红58\",\n" +
            "            \"carGoodsPrice\": 1089.00,\n" +
            "            \"carGoodsPricecount\": 1.00,\n" +
            "            \"carGoodsUserId\": 67531},{},{}],\"payPass\":000000,\"getAddress\":1}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "buyGoods")
    private Json buyGoods(@RequestBody BuyGoodsDTO buyGoodsDTO) {
        Json json = new Json();
        System.out.println(buyGoodsDTO.getCar().toString());
        BuySuccessVO buySuccessVO = userOperationService.buyGoods(buyGoodsDTO.getCar(), buyGoodsDTO.getGetAddress());
        json.setCode(200);
        json.setData(buySuccessVO);
        json.setMessage("购买成功!");
        return json;
    }

    /**
     * 正品验证
     *
     * @return
     */
    @ApiOperation(value = "正品验证",notes = "验证正品 发送商品goodsId 发送 userId 商品购买后的code {\"goodsId\":67531,\"userId\":10082,\"code\":12564}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping("/queryTrueAndFalserf")
    private Json queryTrueAndFalse(@RequestBody Map<String,Integer> map) {
        Json json = new Json();
        Integer goodsId = map.get("goodsId");
        Integer userId = map.get("userId");
        Integer code = map.get("code");
        Integer verify = userOperationService.queryTrueAndFalse(goodsId, userId, code);
        if (verify == 1) {
            json.setCode(200);
            json.setData("真");
            json.setMessage("真");
        }
        if (verify == 0) {
            json.setCode(200);
            json.setData("假");
            json.setMessage("假");
        }
        return json;
    }

    @ApiOperation(value = "获取具体商品信息根据商品Id",notes = "获取具体商品信息根据商品Id Json数据 {\"goodsId\":\"67531\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/queryGoods")
    private Json queryGoods(@RequestBody Map<String,Integer> maps){
        Json json = new Json();
        Integer goodsId = maps.get("goodsId");
        SalenetGoods salenetGoods = userOperationService.queryGoodsById(goodsId);
        json.setData(salenetGoods);
        json.setCode(200);
        json.setMessage("查询成功");
        return json;
    }
}
